<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>new</title>
  <link rel="stylesheet" href="/css/heads.css">
  <link rel="stylesheet" href="/css/news..css">
</head>

<body>
  <div class="nav">
    <ul>
      <li class="pc">
        <a href="index.html">首页</a>
      </li>
      <li class="pc">
        <a href=""> 品牌历史</a>
        <div class="fen">
          <ul class="fen_en">
            <li><a href="index_story.html">一个美丽的故事</a> </li>
            <li>传承人的故事</li>
          </ul>
        </div>
      </li>
      <li class="pc">
        <a href="poins.html">产品中心</a>
        <div class="fen">
          <ul class="fen_en fen1">
            <li><a href=""><a href="poin_sj.html">三角钢琴</a></li>
            <li><a href="poin_ls.html">立式钢琴</a></li>
          </ul>
        </div>
      </li>
      <li class="pc">
        <a href="index_zt.html">展厅</a>
      </li>
      <a class="logo" href="#"></a>
      <li class="pc"><a href="new.html">新闻首页</a></li>
      <li class="pc">服务
        <div class="fen">
          <ul class="fen_en fen2">
            <li><a href="maintain.html">保养与维护</a></li>
            <li><a href="#">经销商查询</a></li>
          </ul>
        </div>
      </li>
      <li class="pc"><a href="">联系</a></li>
      <li class="pc"><a href="lodin.html">个人中心</a></li>
    </ul>
    <span class="ico">
      <em></em>
      <em></em>
      <em></em>
    </span>
  </div>

  <div class="navBou">
    <div>首页</div>
    <div>
      品牌历史
      <p><a href="">个美丽的故事</a></p>
      <p><a href="">传承人的故事</a></p>
    </div>
    <div>
      产品中心
      <p><a href="">三角钢琴</a></p>
      <p><a href="">立式钢琴</a></p>
    </div>
    <div><a href="index_zt.html">展厅</a></div>
    <div><a href="new.html">新闻首页</a></div>
    <div>服务
      <p><a href="">保养与维护</a></p>
      <p><a href="">经销商查询</a></p>
    </div>
  </div>

  <!-- 新闻主题部分 -->
  <div class="news">
    <ul id="newb" class="clearfix">
    </ul>
    <div>
      <div class="btnbox">
        <div>
          <button>1</button>
          <button>2</button>
          <button>3</button>
          <button>4</button>
          <div><input type="text" placeholder="1"><button>确定</button></div>
        </div>
      </div>
    </div>


  </div>
  <script>
    let query = window.location.search.substring(1).split('=');
    let str = query[1]
    var newbox = document.getElementById(newbox);
   
    //监听url并传输数据
    if(query[0]=='page'){
      const xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
          if (xhr.status >= 200 && xhr.status < 300) {
          var result = JSON.parse(xhr.responseText).data;
          var str = newsData(result)
          var cc = newb.innerHTML = str;
        }
      }
    }
    xhr.open('GET', `v1/main/news_fy/${str}`, true);
    xhr.send();
    }
   
    //建立函数传输和处理数据
    function newsData(arr) {
      let str = ``;
      for (let i = 0; i < arr.length; i++) {
        let obj = arr[i];
        let date = new Date(obj.timer);
        let year = date.getFullYear();
        let month = date.getMonth();
        let day = date.getDate();
        /* return console.log(year); */
        str += ` 
        <li class="clearfix">
        <div>
          <img src="/img/${obj.url}.jpg" alt="">
          <div>
            <h2><span>${month + 1}-${day}</span>${year}</h2>
            <p>${obj.tit}</p>
          </div>
        </div>
        </li>
        `;
      }
      return str;
    }
    //监听li的数量来判断ul高度

    //分页点击跳转功能
    {
      let pageBtn=document.querySelectorAll('.btnbox>div>button')
      let index=str-1
      pageBtn[index].className='cur'
      pageBtn.forEach(el=>{
        el.onclick=function(){
          let curPageBtn=document.querySelector('.btnbox>div .cur')
          window.location.href = `new.html?page=${this.innerHTML}`
        }
      })
    }
    //跳转指定页数功能
    {
      let appointPage=document.querySelector('.btnbox > div > div > button')
      appointPage.onclick=function(){
        let page=document.querySelector('.btnbox > div > div > input').value;
        if(page>4){
          page=1
        } 
        window.location.href = `new.html?page=${page}`
      }
    }
  </script>
</body>

</html>